package com.cqc.googleplay.fragment.home;

import java.util.List;

import com.cqc.googleplay.R;
import com.cqc.googleplay.http.HttpHelper;
import com.cqc.googleplay.http.NetUrl;
import com.cqc.googleplay.utils.ColorUtil;
import com.cqc.googleplay.utils.CommonUtil;
import com.cqc.googleplay.utils.DrawableUtil;
import com.cqc.googleplay.utils.FlowLayout;
import com.cqc.googleplay.utils.GsonUtil;
import com.cqc.googleplay.utils.ToastUtil;
import com.cqc.googleplay.view.ContentPager;
import com.google.gson.reflect.TypeToken;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

public class HotFrag extends Fragment {

	private ContentPager contentPager;
	private int text_h_padding;
	private int text_v_padding;
	private int hot_text_radius;
	private FlowLayout flowLayout;
	private ScrollView scrollView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		contentPager = new ContentPager(getActivity()) {

			@Override
			public Object requestData() {
				return getData();
			}

			@Override
			public View createSuccessView() {
				return getSuccessView();
			}
		};
		return contentPager;
	}

	protected View getSuccessView() {

		int flowlayout_padding = (int) CommonUtil.getDimension(R.dimen.flowlayout_padding);
		int flowlayout_h_spacing = (int) CommonUtil.getDimension(R.dimen.flowlayout_h_spacing);
		int flowlayout_v_spacing = (int) CommonUtil.getDimension(R.dimen.flowlayout_v_spacing);

		scrollView = new ScrollView(getActivity());
		flowLayout = new FlowLayout(getActivity());
		flowLayout.setPadding(flowlayout_padding, flowlayout_padding, flowlayout_padding, flowlayout_padding);
		flowLayout.setHorizontalSpacing(flowlayout_h_spacing);
		flowLayout.setVerticalSpacing(flowlayout_v_spacing);

		scrollView.addView(flowLayout);
		return scrollView;
	}

	protected Object getData() {
		text_h_padding = (int) CommonUtil.getDimension(R.dimen.text_h_padding);
		text_v_padding = (int) CommonUtil.getDimension(R.dimen.text_v_padding);
		hot_text_radius = (int) CommonUtil.getDimension(R.dimen.hot_text_radius);

		String json = HttpHelper.get(NetUrl.HOT);
		List<String> list = (List<String>) GsonUtil.jsonToList(json, new TypeToken<List<String>>() {
		}.getType());

		if (list.size() != 0 && list != null) {

			for (int i = 0; i < list.size(); i++) {
				final TextView tv = new TextView(getActivity());
				tv.setPadding(text_h_padding, text_v_padding, text_h_padding, text_v_padding);
				tv.setText(list.get(i));
				tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
				tv.setTextColor(Color.WHITE);
				tv.setGravity(Gravity.CENTER);
				
				GradientDrawable normal = DrawableUtil.getDrawable(ColorUtil.getRandomColor(), hot_text_radius);
				GradientDrawable pressed = DrawableUtil.getDrawable(ColorUtil.getRandomColor(), hot_text_radius);
				tv.setBackgroundDrawable(DrawableUtil.getSelector(pressed, normal));
			
				flowLayout.addView(tv);
				
				tv.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						ToastUtil.showShortToast(tv.getText().toString());
					}
				});
			}
		}
		return list;
	}
}
